var dataUrl = null;
$(function () {
    init();
    // 绑定取消按钮事件
    $(".cancel").click(function () {
        parent.layer.close(parent.pageii);

    });
$("#file").on("change",function () {
    $("#browseFile").val($(this).val())
})
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            //jquery获取复选框值
            if (!checkForm()) {
                return;
            }
            $("#reportName").val($("#reportType option:selected").text());
            // var chk_value =[];//定义一个数组
            var index = layer.load(1, {shade: false});
            $("#submitButton").attr("disabled", "disabled");
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    $("#submitButton").removeAttr("disabled");
                    layer.close(index);
                    if (data == "success") {
                        // parent.location.reload();
                        parent.layer.close(parent.pageii);
                        return false;
                        // });
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    });
});

function init() {
    // dataUrl = $("#dataUrl").val();
    dataUrl ="/modules";
    getProjectName();
    getReportCategory();
    var projectId=$("#projectIdD").val()
    if(projectId!=null&&projectId!=""&&projectId!=undefined){
        getTaskList(projectId)
    }
}
function checkForm() {
    var projectId = $("#projectName").val();
    if (projectId == null || projectId == "" || projectId == undefined) {
        layer.msg("请选择工程名称")
        return false;
    }
    var task = $("#task").val();
    if (task == null || task == "" || task == undefined) {
        layer.msg("请选择任务")
        return false;
    }
    var reportType = $("#reportType").val();
    if (reportType == null || reportType == "" || reportType == undefined) {
        layer.msg("请选择表名")
        return false;
    }
    var file=$("#file").val();
    if(file==null||file==""||file==undefined){
        layer.msg("请选择上传的文件")
        return false;
    }
    return true;
}
/**
 * 获取项目名称
 * @param cropId
 */
function getProjectName() {
    var index = layer.load(1, {shade: false});
    var url = dataUrl + "/homepage/allProjectByPersonIdAndCorpId?personId=" + $("#personId").val() + "&newDate=" + (new Date().getTime());
    ;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'"
                    if($("#projectIdD").val()== result[i].id){
                        html += " selected='selected' ";
                    }
                    html+=">" + (result[i].caption) + "</option>";
                }
                $("#projectName").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
            layer.close(index);
        }
    });
}

/**
 * 获取任务名称
 * @param cropId
 */
function getTaskList(projectId) {
    if (projectId != "") {
        var url = dataUrl + "/basicReport/findByTaskReportList?projectId=" + projectId + "&newDate=" + (new Date().getTime());
        $.ajax({
            url: url,
            method: "get",
            dataType: "json",
            success: function (result) {
                var html = "";
                html += "<option value=''>------请选择------</option>";
                if (result != null) {
                    for (var i = 0; i < result.length; i++) {
                        html += "<option value='" + result[i].id + "'"
                        if($("#taskIdD").val()== result[i].id){
                            html += " selected='selected' ";
                        }
                        html+=">" + (result[i].caption) + "</option>";
                    }
                    $("#task").html(html);
                } else {
                    layer.msg("获取列表信息错误，请联系管理员！");
                }
            }
        });
    } else {
        var html = "";
        html += "<option value=''>------请选择------</option>";
        $("#task").html(html);
    }
}

/**
 * 获取报表类型
 * @param cropId
 */
function getReportCategory() {
    var url = dataUrl + "/basicReport/findReportCategory?type=1&newDate=" + (new Date().getTime());
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>----请选择----</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'"
                    if($("#subTypeD").val()== result[i].id){
                        html += " selected='selected' ";
                    }
                    html += ">" + (result[i].subCaption) + "</option>";
                }
                $("#reportType").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}
/**
 * 上传报表
 */
function uploadReport() {debugger
    var url = dataUrl + "/basicReport/generateReport?type=1&newDate=" + (new Date().getTime());
    var projectId = $("#projectName").val();
    if (projectId != null && projectId != "" && projectId != undefined) {
        url += "&&projectId=" + projectId;
    }
    var task = $("#task").val();
    if (task != null && task != "" && task != undefined) {
        url += "&&taskId=" + task;
    }else{
        layer.msg("请选择任务！");
        return;
    }
    var reportType = $("#reportType").val();
    if (reportType != null && reportType != "" && reportType != undefined) {
        url += "&&subType=" + reportType;
    }else{
        layer.msg("请选择表名");
        return;
    }
    $.ajax({
        url: url,
        method: "get",
        dataType: "text",
        success: function (result) {
            if(result=='success'){
                layer.msg("生成成功");
                findReportVersion();
            }else{
                layer.msg("生成失败");
            }
        }
    });
}